/** 
 * FixsizeSymbolTable.java
 * @author Lex
 * 2007-8-24 ����04:09:00
 */
package fun.symtable;

import org.lex.utils.collections.Collections;

import fun.parser.ast.node.base.Expression;

public class FixsizeSymbolTable extends AbstractSymbolTable {
	private static final FunctionEntry[] EMPTY_FUN = new FunctionEntry[0];
	private static final VariableEntry[] EMPTY_VAR = new VariableEntry[0];

	private FunctionEntry[] functions = EMPTY_FUN;
	private int funIndex = 0;
	private VariableEntry[] variables = EMPTY_VAR;
	private int varIndex = 0;

	public FixsizeSymbolTable(String name, int funCount, int varCount) {
		super(name);
		this.functions = new FunctionEntry[funCount];
		this.variables = new VariableEntry[varCount];
	}

	public VariableEntry getVariable(String name) {
		for (int i = 0; i < varIndex; i++)
			if (this.variables[i].getName().equals(name))
				return this.variables[i];
		return null;
	}

	public VariableEntry setVariable(String name, double value) {
		VariableEntry entry = this.getVariable(name);
		if (null == entry) {
			entry = new VariableEntry(name, value);
			this.variables[varIndex++] = entry;
		}
		entry.setValue(value);
		return entry;
	}

	public FunctionEntry getFunction(String name, int argn) {
		if (null == this.functions || 0 == this.functions.length)
			return this.getParent().getFunction(name, argn);
		for (int i = 0; i < functions.length; i++) {
			FunctionEntry entry = functions[i];
			if (entry.getParameterCount() == argn && entry.getName().equals(name))
				return entry;
		}
		return null;
	}

	public FunctionEntry setFunction(String name, String[] parameters, Expression expr) {
		return null;
	}

	public VariableIterator variables() {
		return new VariableIterator(this, Collections.toIterator(variables, 0, varIndex));
	}

	public FunctionIterator functions() {
		return new FunctionIterator(this, Collections.toIterator(functions, 0, funIndex));
	}
}
